//
//  AppDelegate.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/22.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        //设定tabbar
        self.settingMainTabBar()
        self.initUIBili()
        
        //注册短信验证码
        SMSSDK.registerApp("171834a69d620", withSecret: "fa3e43de2f6eb3f9ab0fd0f29bdbe770")
        
        //开启极光推送
        registerJpush()
        
        return true
 
    }

    //MARK:即将失去活动状态的时候调用(失去焦点, 不可交互)
    func applicationWillResignActive(application: UIApplication) {
        
    }

    //MARK:应用程序进入后台的时候调用
    // 一般在该方法中保存应用程序的数据, 以及状态
    func applicationDidEnterBackground(application: UIApplication) {
        
    }

    //MARK:应用程序即将进入前台的时候调用
    // 一般在该方法中恢复应用程序的数据,以及状态
    func applicationWillEnterForeground(application: UIApplication) {
        
    }

    //MARK:获取焦点(能够和用户交互)
    func applicationDidBecomeActive(application: UIApplication) {
        //每次进入app 将 jpush系统中的角标设为0
        JPUSHService.resetBadge()
        //将系统中设置的角标也清零
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    //MARK:应用程序即将被销毁的时候会调用该方法
    // 注意:如果应用程序处于挂起状态的时候无法调用该方法
    func applicationWillTerminate(application: UIApplication) {
        
    }
    
    func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        //注册 DeviceToken
        JPUSHService.registerDeviceToken(deviceToken)
    }
    
    func application(application: UIApplication,
                     didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print("推送服务配置错误")
    }
}


//MARK:除原来的方法之外，新添加的都写到这个扩展里面
extension AppDelegate{
    //注册jpush推送
    func registerJpush(){
        //设置通知类型
        let userSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert],
                                                      categories: nil)
        //用这个方法可以添加自定义的类型
        JPUSHService.registerForRemoteNotificationTypes(userSettings.types.rawValue,
                                                        categories: nil)
        // 启动JPushSDK
        JPUSHService.setupWithOption(nil, appKey: "7cf01ef59bb896b112d295ed",
                                     channel: "Publish Channel", apsForProduction: false)
        
    }
    
    
    
    //设置window
    func settingWindow(){
        self.window = UIWindow(frame: MainBounds)
        self.window?.backgroundColor = Theme.BGColor
        self.window?.makeKeyAndVisible()
    }
    
    
    //设定tabbar
    func settingMainTabBar(){
        let mainTabBar = MainTabBarController()
        self.window?.rootViewController = mainTabBar
    }
    
    
    //判断当前设备的UI的比例  以6为基本 1
    func initUIBili(){
        switch HeightAPP {
        case iphone4:
            SCALEW = 0.85
            SCALEH = 0.72
        case iphone5:
            SCALEW = 0.85
            SCALEH = 0.85
        case iphone6:
            SCALEW = 1
            SCALEH = 1
        case iphone6p:
            SCALEW = 1.1
            SCALEH = 1.1
        default:
            break
        }
    }
}























